/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.diagram.sequence.sedi.edit;

import java.util.Iterator;
import java.util.StringTokenizer;

import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.emf.common.notify.Notifier;
import org.eclipse.emf.common.util.EList;
import org.eclipse.gef.EditPolicy;
import org.eclipse.gef.GraphicalEditPart;
import org.eclipse.uml2.uml.DurationObservation;
import org.eclipse.uml2.uml.ElementImport;
import org.eclipse.uml2.uml.Stereotype;

import com.cea.papyrus.core.PapyrusPlugin;
import com.cea.papyrus.core.directedit.LabelControlerManager;
import com.cea.papyrus.core.editor.ModelManager;
import com.cea.papyrus.core.editpart.GraphNodeEditPolicy;
import com.cea.papyrus.core.editpart.GraphNodeWithSemanticEditPolicy;
import com.cea.papyrus.core.editpart.NodeNameDisplayEditPart;
import com.cea.papyrus.core.figures.StringFigure;
import com.cea.papyrus.core.helper.ModelerHelper;
import com.cea.papyrus.core.utils.Debug;
import com.cea.papyrus.diagraminterchange2.di2.GraphNode;
import com.cea.papyrus.diagraminterchange2.di2.Property;
import com.cea.papyrus.diagraminterchange2.di2.Uml1SemanticModelBridge;

/**
 * Controler of name edit parts for TimeObservationEditPart.
 */
public class DurationObservationEditPart extends NodeNameDisplayEditPart{


	private static final String AND = "&";
	/** Manager of lightweight label controlers */
	private LabelControlerManager labelsMngr;

	/**
	 * Creates a new PortNameEditPart
	 * @param graphnode the graph node displaying the name of the port
	 * @param modelManager the model manager associated to the model
	 */
	public DurationObservationEditPart(GraphNode graphnode, ModelManager modelManager) {
		super(graphnode,modelManager);
		if( graphnode.getSemanticModel()!= null && ((Uml1SemanticModelBridge)graphnode.getSemanticModel()).getElement()!=null){
			this.element=((Uml1SemanticModelBridge)graphnode.getSemanticModel()).getElement();
		}
		Debug.debug(this,"create DurationObservationEditPart("+ graphnode+")",channel);
	}

	/**
	 * Creates a new PortNameEditPart
	 * @param graphnode the graph node displaying the name of the port
	 * @param element 
	 * @param modelManager the model manager associated to the model 
	 */
	public DurationObservationEditPart(GraphNode graphnode, org.eclipse.uml2.uml.Element element, ModelManager modelManager) {
		super(graphnode,modelManager);
		Debug.debug(this,"create DurationObservationEditPart("+ element+")",channel);
		this.element = element;
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.common.editpart.NodeNameDisplayEditPart#getUmlElement()
	 */
	@Override
	public DurationObservation getUmlElement(){
		return (DurationObservation)super.getUmlElement();
	} 
	protected void createEditPolicies(){
		super.createEditPolicies();
		this.installEditPolicy(EditPolicy.GRAPHICAL_NODE_ROLE, new GraphNodeEditPolicy() );
		this.installEditPolicy( EditPolicy.COMPONENT_ROLE, new GraphNodeWithSemanticEditPolicy(null) );
		
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.common.editpart.NodeNameDisplayEditPart#isElementImport()
	 */
	@Override
	public boolean isElementImport(){
		return false;
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.common.editpart.NodeNameDisplayEditPart#getElementImport()
	 */
	@Override
	public ElementImport getElementImport(){
		return null;
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.common.editpart.NodeNameDisplayEditPart#createFigure()
	 */
	@Override
	protected StringFigure createFigure() {
		return new StringFigure(this.getcompleteLabel());
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.common.editpart.NodeNameDisplayEditPart#activate()
	 */
	@Override
	public void activate() {
		if (isActive()) {
			Debug.debug(this, "should not activate an already active edit part", channel);
			return;
		}
		// start listening for changes in the model
		// FIXME RS bug to check this <=> _adapter
		((Notifier)getGraphNode()).eAdapters().add(_adapter);
		super.activate();
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.common.editpart.NodeNameDisplayEditPart#deactivate()
	 */
	@Override
	public void deactivate() {
		if (!isActive()) {
			Debug.debug(this, "should not deactivate an already deactived edit part", channel);
			return;
		}
		super.deactivate();
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.common.editpart.NodeNameDisplayEditPart#refreshVisuals()
	 */
	@Override
	protected void refreshVisuals() {
		StringFigure f = (StringFigure) getFigure();
		Point p = getGraphNode().getPosition();

		String newText = AND+getUmlElement().getName();
		f.setName(newText);

		Rectangle r = new Rectangle(p ,f.getPreferredSize());
		((GraphicalEditPart) getParent()).setLayoutConstraint(
				this,
				f,
				r);
	}	


	/**
	 * Returns the label to display
	 * @return the label to display
	 */
	public String getLabel() {

		return this.AND+this.getUmlElement().getName();
	}

	/**
	 * Returns the complete label
	 * @return the complete label
	 */
	public String getcompleteLabel() {
		String stereotypesToDisplay="";
		Iterator<Stereotype> iter= this.getUmlElement().getAppliedStereotypes().iterator();

		while(iter.hasNext()){
			Stereotype stereo = iter.next();
			stereotypesToDisplay = stereotypesToDisplay+stereo.getName()+", ";
		}

		if (!(stereotypesToDisplay.equals(""))) {
			stereotypesToDisplay = PapyrusPlugin.ST_LEFT+stereotypesToDisplay.substring(0, stereotypesToDisplay.length()-2)+PapyrusPlugin.ST_RIGHT+"\n";
		}

		return stereotypesToDisplay+ " "+getLabel();

	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.common.editpart.GraphNodeWithSemanticEditPart#stereotypesToDisplay(java.lang.String)
	 */
	@Override
	public String stereotypesToDisplay(String separator) {
		EList displayStereotypesList = ((GraphNode)getGraphNode().getSemanticParent()).getProperty();
		Iterator<Property> iter = displayStereotypesList.iterator();
		String stereotypesToDisplay="";


		while(iter.hasNext()){
			Property prop = iter.next();
			// Bug correction: prop.getKey().equals(XXY) throws a NPE if getKey returns null 
			if(ModelerHelper.PROPERTY_STEREOTYPE_DISPLAY.equals(prop.getKey())){
				String qualifiedName = prop.getValue();

				// property value contains qualifiedName ==> extract name from it
				StringTokenizer strToken = new StringTokenizer(qualifiedName, "::");
				String name="";
				while(strToken.hasMoreTokens()) {
					name = strToken.nextToken();
				}

				stereotypesToDisplay = stereotypesToDisplay+name.substring(0, 1).toLowerCase()+name.substring(1, name.length())+","+separator;
			}
		}
		if (!(stereotypesToDisplay.equals(""))) {
			stereotypesToDisplay = stereotypesToDisplay.substring(0, stereotypesToDisplay.length()-2);
		}

		return stereotypesToDisplay;
	}


}